<template>
  <div class="page-box">

    <el-form class="search" label-width="70px">
      <el-form-item label="品类类型">
        <el-select v-model="listModel.specCategoryType" placeholder="选择品类类型" clearable>
          <el-option v-for="item in specCategoryTypes" :key="item.id" :label="item.typeName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="品类名称">
        <el-input v-model="listModel.specCategoryName" placeholder="品类名称" clearable />
      </el-form-item>
      <el-form-item class="search-btn">
        <el-button type="primary" icon="Search" @click="pageSizeChange">搜索</el-button>
        <el-button icon="RefreshRight" @click="resetList">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="head">
      <span class="title">商品规格品类管理</span>
      <div class="toolbar">
        <el-button type="primary" icon="Plus" @click="addOrModify(null)">添加</el-button>
        <el-button type="primary" icon="Refresh" @click="fetchListData">刷新</el-button>
        <el-button type="danger" icon="Delete" @click="isShowDelDialog = true">批量删除</el-button>
      </div>
    </div>

    <div class="table">
      <el-table
          stripe
          v-loading="loading"
          :data="listData"
          row-key="id"
          @selection-change="selectionChange"
      >
        <el-table-column type="selection" width="30" />
        <el-table-column prop="specCategoryName" label="品类名称" />
        <el-table-column prop="specCategoryType" label="类型" #default="{ row }">
          {{ row.specCategoryType === 1 ? '可搜索' : '不可搜索' }}
        </el-table-column>
        <el-table-column prop="created" label="创建时间" align="right" />
        <el-table-column prop="updated" label="修改时间" />
        <el-table-column label="操作" fixed="right" width="130" align="center" #default="{ row }">
          <el-button type="primary" icon="Edit" @click="addOrModify(row)" />
          <el-button type="danger" icon="Delete" @click="del(row.id)" />
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
        class="pagination"
        v-model:current-page="listModel.current"
        v-model:page-size="listModel.size"
        :page-sizes="[10, 30, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="pageSizeChange"
        @current-change="fetchListData"
    />

    <el-drawer
        size="36%"
        :title="addOrModifyDrawerTitle"
        v-model="isShowAddOrModifyDrawer"
        direction="rtl"
        :before-close="addOrModifyDrawerClose"
        @opened="addOrModifyDrawerOpened(addOrModifyFormRef)"
    >
      <div class="form-content">
        <el-form ref="addOrModifyFormRef" label-width="70px" :model="addOrModifyModel" :rules="addOrModifyRules">
          <el-form-item label="类型" prop="specCategoryType">
            <el-select v-model="addOrModifyModel.specCategoryType" placeholder="选择品类类型" clearable>
              <el-option v-for="item in specCategoryTypes" :key="item.id" :label="item.typeName" :value="item.id" />
            </el-select>
          </el-form-item>
          <el-form-item label="名称" prop="specCategoryName">
            <el-input v-model="addOrModifyModel.specCategoryName" placeholder="规格品类名称" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="doAddOrModify">
              {{ loading ? '正在提交 ...' : '提交' }}
            </el-button>
            <el-button @click="addOrModifyDrawerClose">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>

    <el-dialog
        v-model="isShowDelDialog"
        title="提示"
        width="30%"
        :before-close="delDialogClose"
    >
      <span>确定要删除吗？</span>
      <template #footer>
        <span>
          <el-button @click="delDialogClose">取消</el-button>
          <el-button type="primary" :loading="loading" @click="doDel">
            {{ loading ? '正在删除 ...' : '确定' }}
          </el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { GetListData, AddData, ModifyData, DeleteData, categoryTypes } from '@/api/goods/specCategory'

const loading = ref(false)
const specCategoryTypes = ref(categoryTypes)

/* 列表开始 */
const total = ref(0)
const listModelData = {
  current: 1,
  size: Number(import.meta.env.VITE_PAGE_SIZE),
  specCategoryType: '',
  specCategoryName: '',
}
const listModel = ref({ ...listModelData })
const listData = ref([])

const selectionChange = arr => {
  delModel.value.ids = []
  arr.forEach(row => {
    delModel.value.ids.push(row.id)
  })
}

const fetchListData = async () => {
  if (loading.value) {
    return
  }
  loading.value = true
  const { code, message, data } = await GetListData(listModel.value)
  loading.value = false
  if (code !== 200) {
    ElMessage.error(message)
    return
  }
  listData.value = data.records
  total.value = Number(data.total)
}

const pageSizeChange = () => {
  listModel.value.current = 1
  fetchListData()
}

const resetList = () => {
  listModel.value = { ...listModelData }
  fetchListData()
}

onMounted(() => {
  fetchListData()
})
/* 列表结束 */

/* 添加修改开始 */
const addOrModifyFormRef = ref()
const addOrModifyDrawerTitle = ref('')
const isShowAddOrModifyDrawer = ref(false)

const addOrModifyData = {
  specCategoryType: '',
  specCategoryName: '',
}
const addOrModifyModel = ref({ ...addOrModifyData })

const addOrModifyRules = ref({
  specCategoryType: [{ required: true, message: '请选择品类类型', trigger: 'blur' }],
  specCategoryName: [{ required: true, message: '请输入规格品类名称', trigger: 'blur' }],
})

const addOrModify = (row) => {
  if (row == null) {
    addOrModifyDrawerTitle.value = '添加规格品类'
  } else {
    addOrModifyDrawerTitle.value = '编辑规格品类'
    addOrModifyModel.value = { ...row }
    delete addOrModifyModel.value.created
    delete addOrModifyModel.value.updated
  }
  isShowAddOrModifyDrawer.value = true
}

const doAddOrModify = () => {
  if (loading.value) {
    return
  }
  addOrModifyFormRef.value.validate(async valid => {
    if (!valid) {
      return
    }
    loading.value = true
    let code, message
    if (addOrModifyModel.value.id) {
      const result = await ModifyData(addOrModifyModel.value)
      code = result.code
      message = result.message
    } else {
      const result = await AddData(addOrModifyModel.value)
      code = result.code
      message = result.message
    }
    loading.value = false
    if (code !== 200) {
      ElMessage.error(message)
      return
    }
    isShowAddOrModifyDrawer.value = false
    resetAddOrModify()
    await fetchListData()
    ElMessage.success(message)
  })
}

const addOrModifyDrawerOpened = formEl => {
  formEl.clearValidate()
}

const resetAddOrModify = () => {
  addOrModifyFormRef.value.resetFields()
  addOrModifyModel.value = { ...addOrModifyData }
  loading.value = false
}

const addOrModifyDrawerClose = () => {
  if (loading.value) {
    return
  }
  resetAddOrModify()
  isShowAddOrModifyDrawer.value = false
}
/* 添加修改结束 */

/* 删除开始 */
const isShowDelDialog = ref(false)
const delModel = ref({ ids: [] })

const del = id => {
  delModel.value.ids.push(id)
  isShowDelDialog.value = true
}

const doDel = async () => {
  if (loading.value) {
    return
  }
  if (delModel.value.ids.length <= 0) {
    ElMessage.error('请至少选择一项')
    return
  }
  loading.value = true
  const { code, message } = await DeleteData(delModel.value)
  loading.value = false
  if (code !== 200) {
    ElMessage.error(message)
    return
  }
  isShowDelDialog.value = false
  resetDel()
  await fetchListData()
  ElMessage.success(message)
}

const resetDel = () => {
  delModel.value.ids = []
  loading.value = false
}

const delDialogClose = () => {
  if (loading.value) {
    return
  }
  resetDel()
  isShowDelDialog.value = false
}
/* 删除结束 */
</script>

<style scoped lang="scss">
.page-box {
  width: 100%;
  box-sizing: border-box;
  .search {
    padding: 20px 20px 0;
    background: #fff;
    margin-bottom: 10px;
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      margin: {
        right: 10px;
        bottom: 20px;
      }
    }
    .search-btn {
      margin-left: auto;
    }
    :deep(.el-input-number .el-input__inner) {
      text-align: left;
    }
    :deep(.el-range-editor.el-input__wrapper) {
      box-sizing: border-box;
    }
  }
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px 20px 0;
    background: #fff;
    .title {
      font-size: 16px;
    }
  }
  .table {
    padding: 20px;
    background: #fff;
  }
  .pagination {
    padding: 0 20px 20px;
    background: #fff;
    justify-content: flex-end;
    :last-child {
      margin-right: 0;
    }
  }
}
</style>